package com.zxl.live.screen.screen.ui.widget.panel;

import android.graphics.Bitmap;

import com.zxl.live.screen.screen.ui.widget.surface.BasePanel;
import com.zxl.live.screen.screen.utils.ScreenUtils;

import java.util.Random;

public abstract class ScreenSpirit extends BasePanel {
	
	public Bitmap bitmap;
	
	public float x;
	
	public float y;

	public float speed;
	
	public int angle;
	
	public boolean isActive;
	
	public int alpha;
	
	public int alphaSpeed;
	
	public boolean isAlphaAdd;
	
	public int tmp;
	
	public float baseX;
	
	public float baseY;
	
	public float rotateX;
	
	public float rotateY;
	
	public float rotateXAdd;
	
	public float rotateYAdd;
	
	public boolean isRotateXAdd;
	
	public boolean isRotateYadd;
	
	public int rotateAngle;
	
	public float rotateAngleAdd;
	
	public boolean isRotateAngleAdd;
	
	public int flyingAngle;
	
	public float scale;
	
	public Random mRandom;
	
	
	public ScreenSpirit(Bitmap bitmap, Random mRandom) {
		this.bitmap = bitmap;
		this.mRandom = mRandom;
		reset();
	}
	
	public void changeAngle() {
		angle += 2;
		if (angle > 360) {
			angle = 0;
		}
	}
	
	public void changeAlpha() {
		if (isAlphaAdd) {
			alpha += alphaSpeed;
			if (alpha > 255) {
				alpha = 255;
				isAlphaAdd = false;
			}
		} else {
			alpha -= alphaSpeed;
			if (alpha < 0) {
				alpha = 0;
				isAlphaAdd = true;
			}
		}
	}
	
	public void changeX() {
		tmp++;
		if (tmp > 30) {
			tmp = 0;
			flyingAngle = mRandom.nextInt(360);
		}
		x += (Math.sin((float) flyingAngle / 180 * Math.PI) * 0.25f) + 1f;
	}

	public void changeY() {
		y += speed;
	}
	
	public void changeRotateAngle() {
		rotateAngle += rotateAngleAdd;
		if (rotateAngle > 360) {
			rotateAngle = rotateAngle % 360;
		}
	}

	public void changeRotate() {
		if (isRotateXAdd) {
			rotateX += rotateXAdd;
			if (rotateX > 80) {
				rotateX = 80;
				isRotateXAdd = false;
			}
		} else {
			rotateX -= rotateXAdd;
			if (rotateX < -80) {
				rotateX = -80;
				isRotateXAdd = true;
			}
		}

		if (isRotateYadd) {
			rotateY += rotateYAdd;
			if (rotateY > 80) {
				rotateY = 80;
				isRotateYadd = false;
			}
		} else {
			rotateY -= rotateYAdd;
			if (rotateY < -80) {
				rotateY = -80;
				isRotateYadd = true;
			}
		}
	}
	
	public void reset() {
		if (null == mRandom) {
			return;
		}
		isActive = true;
		baseX = 0;
		baseY = -mRandom.nextInt(ScreenUtils.sBaseHeightPixels);
		x = -ScreenUtils.sBaseWidthPixels / 2 + mRandom.nextInt((int) (ScreenUtils.sBaseWidthPixels * 1.5f));
		if (x < -ScreenUtils.sBaseWidthPixels / 4) {
			y = mRandom.nextInt(ScreenUtils.sBaseHeightPixels / 4);
		} else {
			y = -mRandom.nextInt(ScreenUtils.sBaseHeightPixels / 2);
		}
		alpha = 125;
		isAlphaAdd = mRandom.nextBoolean();
		alphaSpeed = 1 + mRandom.nextInt(6);
		rotateAngle = 0;
		rotateAngleAdd = 1 + mRandom.nextInt(40) * 0.1f;
		isRotateAngleAdd = mRandom.nextBoolean();
		flyingAngle = mRandom.nextInt(360);

		speed = 3f + mRandom.nextInt(40) * 0.1f;
		scale = (0.75f + 0.01f * mRandom.nextInt(50)) * 0.8f;

		rotateX = -80 + mRandom.nextInt(160);
		rotateY = -80 + mRandom.nextInt(160);
		rotateXAdd = 0.5f + mRandom.nextInt(30) * 0.1f;
		rotateYAdd = 0.5f + mRandom.nextInt(30) * 0.1f;
		isRotateXAdd = mRandom.nextBoolean();
		isRotateYadd = mRandom.nextBoolean();
	}
}
